trigger NumberOfReschedules on Event (after update) {
      List<Event> myEvent = [select id,RecordtypeId ,StartDateTime,EndDateTime ,Description,Appointment_type__c,Number_of_Reschedules__c,Reschedule_Event__c,WhoId,WhatId from event where id in:trigger.newMap.keyset()];
      List<Event> EventlistObj = new list<Event>();
      List<Notes__c> listObj = new List<Notes__c>();
      Map<ID, DateTime> myMapStart = new Map<ID, DateTime>();
      Map<ID, DateTime> myMapEnd = new Map<ID, DateTime>();
      ID localAdvertiserRecordTypeID = RecordTypeHelper.GetRecordTypeID('Event', 'Local Advertiser Event');
      for (Event oldEvent : trigger.old){
            myMapStart.put(oldEvent.ID, oldEvent.StartDateTime);
            myMapEnd.put(oldEvent.ID, oldEvent.EndDateTime); 
      }
      
      for (Event theEvent : myEvent){
            if ((theEvent.StartDateTime != myMapStart.get(theEvent.Id)|| theEvent.EndDateTime != myMapEnd.get(theEvent.Id))  && theEvent.RecordtypeId == localAdvertiserRecordTypeID ) {
                  String LdWhoId =theEvent.WhoId;
                  String LdWhatId=theEvent.Whatid;
                  theEvent.Number_of_Reschedules__c += 1;
                  theEvent.Reschedule_Event__c = true;
                        Notes__c calln= new Notes__c(); 
                        if(theEvent.WhoId !=null){
                        if(LdWhoId.startsWith('00Q') ){                       
                            calln.Lead__c = theEvent.WhoId;
                            calln.type__c = 'Lead';}}
                         else if(theEvent.WhatId != null && LdWhatId.startsWith('001')){                        
                             calln.Account__c = theEvent.WhatId;
                             calln.type__c = 'Renewal';
                            }
                        calln.Notes__c = 'Appointment Rescheduled: ' + '\n' + theEvent.Description;           
                        calln.Disposition__c = theEvent.Appointment_type__c;
                        listObj.add(calln);
                        EventlistObj.add(theEvent);                        
            }
      }
                        if(listObj.size()>0)
                        {    
                            try{insert listObj;}
                            catch(DmlException e){system.debug(e);}
                            catch(Exception e){system.debug(e);}    
                        } 
                        try{update EventlistObj;}
                        catch(DmlException e){system.debug(e);}


}